package org.mms.service.impl;

import org.mms.exception.BusinessException;
import org.mms.pojo.dto.VehicleDto;
import org.mms.pojo.entity.Vehicle;
import org.mms.pojo.request.VehicleRequest;
import org.mms.repository.VehicleRepository;
import org.mms.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleRepository vehicleRepository;

    @Override
    public List<Vehicle> list() {
        return vehicleRepository.findAll();
    }

    @Override
    public Vehicle getById(Long id) {
        return vehicleRepository.findById(id)
                .orElseThrow(() -> new BusinessException("车辆不存在"));
    }

    @Override
    public Vehicle create(VehicleRequest request) {
        Vehicle entity = new Vehicle();
        BeanUtils.copyProperties(request, entity);
        return vehicleRepository.save(entity);
    }

    @Override
    public Vehicle update(VehicleRequest request) {
        Vehicle entity = vehicleRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("车辆不存在"));
        BeanUtils.copyProperties(request, entity);
        return vehicleRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        vehicleRepository.deleteById(id);
    }
}